package com.example.restatr.Atcivity.CitySubway;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.restatr.Atcivity.CitySubway.Bean.SevenDayWeatherBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

public class SevenWeatherAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private RecyclerView rv2;
    private LineChart lineChart;
    private List<Entry> maxTemp = new ArrayList<>();//最高温度数据链表
    private List<Entry> minTemp = new ArrayList<>();//最低温度数据链表
    private List<SevenDayWeatherBean.DataBean.WeatherListBean> weatherList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seven_weather_at);
        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        rv2 = findViewById(R.id.rv2);
        lineChart = findViewById(R.id.lineChart);
        Tools.SetBarTitle(toolbar, barTitle, "最近7天天气", search, this, false);
        weatherList = new Gson().fromJson(getIntent().getStringExtra("data"), new TypeToken<List<SevenDayWeatherBean.DataBean.WeatherListBean>>() {
        }.getType());
        rv2.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        setChartStyle();
        rv2.setAdapter(new sevenWeatherAdapter());
        for (int i = 0; i < weatherList.size(); i++) {
            maxTemp.add(new Entry(i, Float.valueOf(weatherList.get(i).getMaxTemperature())));
            minTemp.add(new Entry(i, Float.valueOf(weatherList.get(i).getMinTemperature())));
        }
        LineDataSet maxTempDataSet = new LineDataSet(maxTemp, "");//最高温度数据集，即最高温度线条
        LineDataSet mixTempDataSet = new LineDataSet(minTemp, "");//最低温度数据集，即最低温度线条
        //设置数据集样式
        setDataSetStyle(maxTempDataSet);
        setDataSetStyle(mixTempDataSet);
        LineData lineData = new LineData(mixTempDataSet);
        lineData.addDataSet(maxTempDataSet);
        lineChart.setData(lineData);
        lineChart.invalidate();//刷新图表
    }

    private void setChartStyle() {
        lineChart.getXAxis().setEnabled(false);//隐藏X轴
        lineChart.getAxisRight().setEnabled(false);//隐藏右边的Y轴
        lineChart.getAxisLeft().setEnabled(false);//隐藏左边的Y轴
        lineChart.getLegend().setEnabled(false);//隐藏图例
        lineChart.setTouchEnabled(false);//关闭所有触摸事件
        //#region 隐藏描述
        Description description = new Description();
        description.setText("");
        lineChart.setDescription(description);
        //#endregion
    }

    private void setDataSetStyle(LineDataSet lineDataSet) {
        //region 字体 样式
        lineDataSet.setValueTextColor(Color.parseColor("#728A87"));
        lineDataSet.setValueTextSize(13);//设置数据字体大小
        //endregion
        //region 线 样式
        lineDataSet.setColor(Color.parseColor("#728A87"));//设置线条颜色
        lineDataSet.setLineWidth(2);//线条粗细
        //endregion
        //region 圆 样式
        lineDataSet.setCircleColor(Color.parseColor("#728A87"));//设置圆颜色
        lineDataSet.setCircleRadius(4f);//设置圆半径
        lineDataSet.setDrawCircleHole(true);//开启空心圆
        //endregion
        //region 设置数据格式
        lineDataSet.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return Math.round(value) + "℃";
            }
        });
        //endregion
        lineDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);//设置线条模式，水平的贝塞尔曲线
    }

    class sevenWeatherAdapter extends RecyclerView.Adapter<sevenWeatherAdapter.viewHolder> {

        @NonNull
        @Override
        public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.seven_weather_item_layout, parent, false);
            return new viewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull viewHolder holder, int position) {
            holder.label.setText(weatherList.get(position).getDay().substring(6));
            holder.day.setText(weatherList.get(position).getDay().substring(0, 6));
            if (weatherList.get(position).getWeather().equals("晴")) {
                holder.weatherIcon.setImageResource(R.drawable.ic_round_wb_sunny_24);
            } else {
                holder.weatherIcon.setImageResource(R.drawable.ic_round_wb_cloudy_24);
            }
            holder.weather.setText(weatherList.get(position).getWeather());

        }

        @Override
        public int getItemCount() {
            return weatherList.size();
        }

        class viewHolder extends RecyclerView.ViewHolder {
            private TextView label;
            private TextView day;
            private ImageView weatherIcon;
            private TextView weather;

            public viewHolder(@NonNull View itemView) {
                super(itemView);
                label = itemView.findViewById(R.id.label);
                day = itemView.findViewById(R.id.day);
                weatherIcon = itemView.findViewById(R.id.weatherIcon);
                weather = itemView.findViewById(R.id.weather);
            }
        }
    }

}